package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityGoodsOrderRefundAuditDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityCoupon;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderRefundAudit;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderRefundAudit.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderRefundAuditExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderRefundAuditExample.Criteria;
import com.ecoolex.ying.space.enums.EstateProjectActivityGoodsOrderRefundAuditStatus;
import com.ecoolex.ying.space.enums.EstateProjectActivityGoodsOrderStatus;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityGoodsOrderRefundAuditManager {
	
    @Autowired
    private EstateProjectActivityGoodsOrderRefundAuditDAO dao;
    
    @Autowired
    private EstateProjectActivityCouponPackageManager couponPackageManager;
    
    @Autowired
    private EstateProjectActivityGoodsOrderManager orderManager;
    
    public void add(EstateProjectActivityGoodsOrder record,String updateUser) {
    	EstateProjectActivityGoodsOrderRefundAudit audit = new EstateProjectActivityGoodsOrderRefundAudit();
    	if(Check.notNull(record.getCouponCode())) {
    		EstateProjectActivityCoupon coupon = couponPackageManager.getCoupon(record.getCouponCode());
    		audit.setRefundPrice(record.getRealPrice().add(coupon.getSalePrice()));
    	}else {
    		audit.setRefundPrice(record.getRealPrice());
    	}
    	audit.setInitiateRefundUserId(record.getOperationUserId());
    	audit.setOrderId(record.getOrderId());
    	audit.setOrderNo(record.getOrderNo());
    	audit.setStatus(EstateProjectActivityGoodsOrderRefundAuditStatus.WAITING_AUDIT.real());
    	audit.setDeleted(EstateProjectActivityGoodsOrder.NOT_DELETED);
    	audit.setCreateUser(updateUser);
    	audit.setCreateTime(Date.from(Instant.now()));
    	audit.setUpdateUser(updateUser);
    	audit.setUpdateTime(Date.from(Instant.now()));
    	dao.insert(audit);
    }
    
    public void auditPassed(Integer auditId,String remark,Integer auditUserId,String updateUser) {
    	EstateProjectActivityGoodsOrderRefundAudit refundAudit = get(auditId);
    	BizAssert.isTrue(Check.notNull(refundAudit),ResultCode.VERIFY.build(10), "退款审核记录不存在");
    	BizAssert.isTrue(EstateProjectActivityGoodsOrderRefundAuditStatus.canAudit(refundAudit),ResultCode.VERIFY.build(11), "记录已经审核");
    	refundAudit.setStatus(EstateProjectActivityGoodsOrderRefundAuditStatus.PASSED.real());
    	refundAudit.setAuditUserId(auditUserId);
    	refundAudit.setRemark(remark);
    	refundAudit.setUpdateUser(updateUser);
    	refundAudit.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(refundAudit, Column.status,Column.remark,Column.updateUser,Column.updateTime);
    	orderManager.updateStatus(refundAudit.getOrderId(), EstateProjectActivityGoodsOrderStatus.REFUND.real(), updateUser);
    }
    
    public void auditRefused(Integer auditId,String remark,Integer auditUserId,String updateUser) {
    	EstateProjectActivityGoodsOrderRefundAudit refundAudit = get(auditId);
    	BizAssert.isTrue(Check.notNull(refundAudit),ResultCode.VERIFY.build(10), "退款审核记录不存在");
    	BizAssert.isTrue(EstateProjectActivityGoodsOrderRefundAuditStatus.canAudit(refundAudit),ResultCode.VERIFY.build(11), "记录已经审核");
    	refundAudit.setStatus(EstateProjectActivityGoodsOrderRefundAuditStatus.REFUSED.real());
    	refundAudit.setAuditUserId(auditUserId);
    	refundAudit.setRemark(remark);
    	refundAudit.setUpdateUser(updateUser);
    	refundAudit.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(refundAudit, Column.status,Column.remark,Column.updateUser,Column.updateTime);
    	orderManager.updateStatus(refundAudit.getOrderId(), EstateProjectActivityGoodsOrderStatus.PAID.real(), updateUser);
    }
    
    public EstateProjectActivityGoodsOrderRefundAudit get(Integer auditId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(auditId, EstateProjectActivityGoodsOrderRefundAudit.NOT_DELETED);
    }
    
    public Pagination<EstateProjectActivityGoodsOrderRefundAudit> query(Integer status,Sorting[] sorts,Page page){
    	EstateProjectActivityGoodsOrderRefundAuditExample example
			= new EstateProjectActivityGoodsOrderRefundAuditExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNull(status)) {
    		criteria.andStatusEqualTo(status);
    	}
    	criteria.andDeletedEqualTo(EstateProjectActivityGoodsOrderRefundAudit.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
    	return Pagination.create(page, 
    		dao.countByExample(example),
    		() -> dao.selectByExample(example));
    }
    
}